<template>
  <GoodsList :value="goodsList"
             :waybill="waybill"
             @input="handleInput"
             :addition="addition"
             :total-data="totalData"
             :readonly="readonly" />
</template>

<script>
import _ from "lodash";
import WaybillMixin, { STATUS_CODES } from "./waybill.mixin";
import GoodsList from "@/components/basic/GoodsList.vue";

export default {
  name: "WaybillGoodsList",
  components: { GoodsList },
  mixins: [WaybillMixin],
  props: {
    isAll: [Boolean, Number],
    oneBillFlag: [Boolean, String],
    isClose: [Boolean, String]
  },
  data () {
    return {};
  },
  computed: {
    status () {
      return this.waybill?.status;
    },
    addition () {
      const { volumeAddition: volume, weightAddition: weight } =
        this.waybill.waybillCache || {};
      return { volume, weight, goodsName: "补差" };
    },
    totalData () {
      const { calculateVolume: volume, calculateWeight: weight, totalQuantity: quantity } =
        this.waybill.waybillCache || {};
      return { volume: volume && volume.toFixed(2), weight: weight && weight.toFixed(2), quantity, goodsName: "合计" };
    },
    goodsReceipt () {
      return ({ goodsReceipt = {} }) => {
        if (this.status !== STATUS_CODES.RECEIPTED) {
          return {};
        }
        const { quantity: signQuantity } = goodsReceipt;
        return { ...goodsReceipt, signQuantity };
      };
    },
    goodsSign () {
      return (goods) => {
        const goodsSign = goods.goodsSign || {};
        const { quantity: signQuantity } = goodsSign;
        return { ...goodsSign, signQuantity };
      };
    },
    goodsList () {
      return this.waybill.goodsList?.map((goods) => {
        const { quantity } = goods;
        const goodsSign = this.goodsSign(goods);
        const goodsReceipt = this.goodsReceipt(goods);
        const signQuantity = goodsReceipt.quantity || (goodsReceipt.quantity === 0 ? 0 : goodsSign.quantity) || (goodsSign.quantity === 0 ? 0 : quantity);
        return {
          ...goods,
          barcode: goods.materialCode,
          ...goodsSign,
          ...goodsReceipt,
          goodsSign,
          signQuantity,
          quantity
        };
      });
    },
    isSignNumOver () {
      return ({ signQuantity, quantity }) => Number(signQuantity) > quantity;
    },
    isSignNumZero () {
      return ({ signQuantity }) => {
        return !signQuantity && signQuantity !== 0 && signQuantity !== "0";
      };
    },
    isSignNumLack () {
      return ({ signQuantity, exceptionQuantity, quantity }) =>
        Number(signQuantity) + Number(exceptionQuantity) < quantity;
    },
    hasCloseBacklog () {
      // 有关单待办才显示关单审核按钮
      return this.waybill.close?.status === 20;
    },
    readonly () {
      return (this.status !== STATUS_CODES.WAIT_SIGN && this.status !== STATUS_CODES.WAIT_RECEIPT) || !!this.isAll || !!this.oneBillFlag || !!this.isClose;
    }
  },
  methods: {
    checkSignNums: _.debounce(function (goods) {
      if (goods.isSignNumOver) {
        this.$message.warning("实签数量不能大于物品数量！");
        return;
      }

      if (goods.isSignNumLack) {
        this.$message.warning("实签数量+异常数量不能小于物品数量！");
        return;
      }

      if (goods.isSignNumZero) {
        this.$message.warning("实签数量不能为空！");
        return;
      }
    }, 500),
    handleInput (goodsList) {
      let flag = false;
      let formattedEx = {}
      const nums = goodsList.map((goods) => {
        const formatted = {
          goodsId: Number(goods.goodsId),
          quantity: Number(goods.quantity) || 0,
          signQuantity: Number(goods.signQuantity) || 0,
          damageQuantity: Number(goods.damageQuantity) || 0,
          exceptionDesc: goods.exceptionDesc,
          lackQuantity: Number(goods.lackQuantity) || 0,
          loseQuantity: Number(goods.loseQuantity) || 0,
          otherQuantity: Number(goods.otherQuantity) || 0,
          exceptionQuantity: Number(goods.exceptionQuantity) || 0,
          waybillGoodsId: goods.waybillGoodsId,
          isSignNumOver: this.isSignNumOver(goods),
          isSignNumZero: this.isSignNumZero(goods),
          isSignNumLack: this.isSignNumLack(goods)
        };
        if (formatted.isSignNumOver || formatted.isSignNumZero || formatted.isSignNumLack) {
          flag = true;
          formattedEx = formatted
        }
        return formatted;
      });
      if (flag) {
        this.checkSignNums(formattedEx);
      }
      this.$emit("input", nums);
    }
  }
};
</script>
